package com.celinux.ce;

import cn.hutool.core.map.MapUtil;
import com.celinux.ce.pojo.SysUser;
import com.celinux.ce.service.SysUserService;
import com.celinux.ce.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.UUID;

/**
 * @Author: llq
 * @Date: 2020/5/27
 */
@RestController
@RequestMapping("/get")
public class CeController {

    @Value("${aaaa.bb}")
    String bb;
    @Value("${web.upload-path}")
    private String uploadPath;
    @Value("${web.basePath}")
    private String basePath;

    @Autowired
    SysUserService sysUserService;

    @GetMapping("/ce")
    public String CeService(){
        return "你好啊!!!,docker"+bb;
    }


    @PostMapping("/insert")
    public String insertUser(@RequestParam Map<String,Object> param){
        String username = MapUtil.getStr(param, "username");
        String password = MapUtil.getStr(param, "password");
        String image = MapUtil.getStr(param, "image");

        SysUser sysUser = new SysUser();
        sysUser.setUsername(username);
        sysUser.setPassword(password);
        sysUser.setImage(image);

        boolean insert = sysUserService.insert(sysUser);
        return "存："+insert;
    }

    @GetMapping("/select")
    public SysUser selectUser(@RequestParam Map<String,Object> param){
        Long id = MapUtil.getLong(param, "id");
        SysUser sysUser = sysUserService.selectById(id);
        return sysUser;
    }




    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/uploadImage")
    public String upload(@RequestParam("file") MultipartFile file) {

        System.out.println("111");

        if (!file.isEmpty()) {
            //获取文件名称，包含后缀
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String newFileName = UUID.randomUUID() + suffix;
            //放在static下的原因是：存在的是静态文件资源，即通过浏览器输入本地服务器地址，加文件名时就可以访问的
            String path = uploadPath;
            try {
                FileUtils.fileupload(file.getBytes(), path, newFileName);
            } catch (Exception e) {
                e.printStackTrace();
                return "上传失败！";
            }
            return basePath + newFileName;

        }
        return "请上传文件！";
    }


}
